import { dbUtils } from '../core'
import type { User } from '../types'

export const credentialRepository = {
    upsert(username: string, password: string, userId: string): void {
        dbUtils.execute(
            `INSERT INTO users (username, password, userId)
             VALUES (?, ?, ?)
             ON CONFLICT(username) DO UPDATE SET
               password=excluded.password,
               userId=excluded.userId,
               updated_at=CURRENT_TIMESTAMP`,
            [username, password, userId]
        )
    },

    verify(username: string, password: string): User | null {
        const row = dbUtils.queryOne(
            `SELECT * FROM users WHERE username = ? AND password = ? LIMIT 1`,
            [username, password]
        )
        return row
    }
}

export default credentialRepository


